﻿var Crazy250 = new Object();
var LRTabs;
Crazy250.SessionId = $.cookie("Crazy250Session");
Crazy250.UserId = $.cookie("Crazy250UserId");
Crazy250.NickName = $.cookie("Crazy250NickName");
Crazy250.CheckLogin = function() {
    if (Crazy250.SessionId == null || Crazy250.UserId == null || Crazy250.NickName == null
        || Crazy250.SessionId == "" || Crazy250.UserId == "" || Crazy250.NickName == "") {

        LRTabs = $("#LRTabs").tabs();
        $("#LRTabs").show();
    }
    else {

        var CheckLoginInfo = new Array();
        CheckLoginInfo.push({ "Key": "Method", "Value": "CheckLogin" });
        CheckLoginInfo.push({ "Key": "SessionId", "Value": Crazy250.SessionId });
        CheckLoginInfo.push({ "Key": "UserId", "Value": Crazy250.UserId });
        var result = Crazy250.Ajax(CheckLoginInfo, false, null, null, true);
        if (result.ResultCode == "0") {
            $("#MainDiv").show();
            $("#NickNameLB").text(Crazy250.NickName);
            Crazy250.InitRes();
        }
        else {
            LRTabs = $("#LRTabs").tabs();
            $("#LRTabs").show();
        }
    }
}
Crazy250.Register = function() {
    if ($("#RegisterTab").validate()) {
        var RegisterInfo = new Array();
        RegisterInfo.push({ "Key": "Method", "Value": "Register" });
        RegisterInfo.push({ "Key": "Email", "Value": $("#RegisterEmail").val() });
        RegisterInfo.push({ "Key": "Password", "Value": $("#RegisterPassword").val() });
        RegisterInfo.push({ "Key": "NickName", "Value": $("#RegisterNickName").val() });
        var result = Crazy250.Ajax(RegisterInfo);
        if (result.ResultCode == "0") {
            alert("注册成功，登录即可进入游戏！")
            LRTabs.tabs("option", "active", 0);
            LRTabs.hide();
            $("#RegisterEmail").val("");
            $("#RegisterPassword").val("");
            $("#RegisterNickName").val("");
            $("#MainDiv").show();
        }
    }
}
Crazy250.Login = function() {
    if ($("#LoginTab").validate()) {
        var LoginInfo = new Array();
        LoginInfo.push({ "Key": "Method", "Value": "Login" });
        LoginInfo.push({ "Key": "Email", "Value": $("#LoginEmail").val() });
        LoginInfo.push({ "Key": "Password", "Value": $("#LoginPassword").val() });
        var result = Crazy250.Ajax(LoginInfo);
        if (result.ResultCode == "0") {
            Crazy250.SessionId = Crazy250.GetValue(result.DataList, 'SessionId');
            Crazy250.UserId = Crazy250.GetValue(result.DataList, 'UserId');
            Crazy250.NickName = Crazy250.GetValue(result.DataList, 'NickName');
            $.cookie("Crazy250Session", Crazy250.SessionId);
            $.cookie("Crazy250UserId", Crazy250.UserId);
            $.cookie("Crazy250NickName", Crazy250.NickName);
            $("#NickNameLB").text(Crazy250.NickName);
            LRTabs.hide();
            $("#MainDiv").show();
            Crazy250.InitRes();
        }
    }
}
Crazy250.GetValue = function(dataList, key) {
    var value = "";
    $(dataList).each(function(index) {
        if (this.Key.toUpperCase() == key.toUpperCase()) {
            value = this.Value;
            return false;
        }
    });
    return value;
}
Crazy250.Ajax = function(APIInputList, async, success, error, alertError) {
    var wsdata = JSON.stringify(APIInputList);
    var isasync = async ? true : false;
    if (isasync) {
        var request = $.ajax({
            async: isasync,
            type: "post",
            contentType: "application/json",
            url: "Crazy250API.asmx/QueryAPI",
            data: "{'InputList':" + wsdata + "}",
            dataType: "json",
            success: function(result) {
                alert(result.d.ResultCode);
            },
            error: function(jqXHR, textStatus) {

                alert("Request failed: " + jqXHR.responseText);
            }
        })
    }
    else {
        var request = $.ajax({
            async: isasync,
            type: "post",
            contentType: "application/json",
            url: "Crazy250API.asmx/QueryAPI",
            data: "{'InputList':" + wsdata + "}",
            dataType: "json"
        })
        if (request.responseJSON.d.ResultCode != "0")
            alert(request.responseJSON.d.ResultMessage);
        return request.responseJSON.d;
    }
}
$(document).ready(function() {
    Crazy250.CheckLogin();
    $(document).tooltip();
});
